//
//  ENMyDynamicViewController.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/8/9.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "ENMyDynamicViewController.h"
#import "ENDynamicPlistManager.h"

@interface ENMyDynamicViewController ()<
UITableViewDelegate,
UITableViewDataSource>

@property (weak, nonatomic) IBOutlet LPCTableView *tableView;

@end

@implementation ENMyDynamicViewController
{
    NSMutableArray *_arrayData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)initData
{
    _arrayData = [[NSMutableArray alloc] initWithArray:[ENDynamicPlistManager loadAllDynamic]];
}

- (void)updateUI
{
    [self updateNavBar];
    
    self.view.backgroundColor = COLOR_Background_TableView;
    [_tableView reloadData];
}

- (void)updateNavBar
{
    self.title = @"消息通知";
    
    if (_arrayData && _arrayData.count > 0) {
        [self addClearNavBtn];
    }else {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (void)addClearNavBtn
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:ColorWhite forState:UIControlStateNormal];
    [btn setTitle:@"清空" forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_PFSC_Medium(16);
    [btn addTarget:self action:@selector(clearBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)clearBtnAction
{
    if ([ENDynamicPlistManager clearDynamic]) {
        _arrayData = nil;
        [self updateNavBar];
        [_tableView reloadData];
        [LPCMBProgressHUD showSuccessWithText:@"清空成功"];
    }else {
        [LPCMBProgressHUD showErrorWithText:@"清空失败"];
    }
}


#pragma mark - UITableViewDelegate、UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPCTableViewCell *cell = XIBcellFromMainBundle(@"ENDynamicCell");
    
    [cell updateData:_arrayData[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}


@end
